package com.czl.module_storehouse.activity.intostore.selectgoods;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.managegoods.home.ManageHomeActivity;
import com.czl.module_storehouse.adapter.SelectGoodsAdapter;
import com.czl.module_storehouse.databinding.FooterSelectedGoodsBinding;
import com.czl.module_storehouse.databinding.HeaderManageHomeBinding;
import com.czl.module_storehouse.event.AddWareHouseEvent;
import com.czl.module_storehouse.event.OperationEvent;
import com.czl.module_storehouse.event.SelectGoodsEvent;
import com.czl.module_storehouse.event.ShiftEvent;
import com.czl.module_storehouse.mvp.presenter.CategoryPresenter;
import com.czl.module_storehouse.mvp.presenter.SortPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Map;

/**
 * 选择物品页面
 *
 * @author xubin
 */
public class SelectGoodsActivity extends ManageHomeActivity implements SimpleView {
    private SelectGoodsAdapter mSelectGoodsAdapter;

    @InjectPresenter
    CategoryPresenter mCategoryPresenter;
    @InjectPresenter
    SortPresenter mSortPresenter;
    private FooterSelectedGoodsBinding mSelectedGoodsBinding;
    private boolean isUseOldSource;

    @Override
    public SortPresenter getSortPresenter() {
        return mSortPresenter;
    }

    @Override
    public CategoryPresenter getCategoryPresenter() {
        return mCategoryPresenter;
    }

    @Override
    protected HeaderManageHomeBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return HeaderManageHomeBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        boolean isHiddenAddGoods = getIntent().getBooleanExtra("isHiddenAddGoods", false);
        mSelectedGoodsBinding = FooterSelectedGoodsBinding.inflate(getLayoutInflater(), binding.containerBottom, true);
        mChildBinding.layoutSelected.llCommonAdd.setVisibility(isHiddenAddGoods ? View.GONE : View.VISIBLE);
        mSelectedGoodsBinding.textAdd.setOnClickListener(v -> submit());
        super.initData(savedInstanceState);
        mToolBinding.toolbarRightText.setVisibility(View.GONE);
        mToolBinding.toolbarContentTitle.setText("选择物品");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public SelectGoodsAdapter getAdapter() {
        if (mSelectGoodsAdapter == null) {
            mSelectGoodsAdapter = new SelectGoodsAdapter(R.layout.item_select_goods2, new ArrayList<>());
            mSelectGoodsAdapter.setOnItemClickListener((adapter, view, position) -> {
                int size = mSelectGoodsAdapter.getSelectedMap().size();
                setTvCount(size);
            });
            mSelectGoodsAdapter.setOnSelectedItemChangeListener((position, sortBean) -> {
                int size = mSelectGoodsAdapter.getSelectedMap().size();
                setTvCount(size);
            });
        }
        return mSelectGoodsAdapter;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(ShiftEvent event) {
        if (event == null) {
            return;
        }
        isUseOldSource = true;
        mPasteCode = event.getPasteCode();
        getAdapter().setSelectedList(event.getSortBeanList());
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(OperationEvent event) {
        if (event == null) {
            return;
        }
        isUseOldSource = true;
        mPasteCode = event.getPasteCode();
        getAdapter().setSelectedList(event.getSortBeanList());
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AddWareHouseEvent event) {
        if (event == null) {
            return;
        }
        isUseOldSource = true;
        getAdapter().setSelectedList(event.getSortBeanList());
    }

    private void setTvCount(int count) {
        mSelectedGoodsBinding.textAdd.setEnabled(count > 0);
        String text;
        text = "已选择物品" + count;
        mSelectedGoodsBinding.tvSortCount.setText(text);
    }

    @Override
    protected void loadData() {
        if (mSortPresenter != null) {
            mPriceInfo = 0;
            String name = mChildBinding.etSearch.getText() == null ? null : mChildBinding.etSearch.getText().toString();
            mSortPresenter.getSortList(true, mPasteCode, ids, mPriceInfo, name);
        }
    }

    private void submit() {
        SelectGoodsEvent event = new SelectGoodsEvent();
        if (isUseOldSource) {
            event.setSortBeanList(mSelectGoodsAdapter.getSelectedNewList());
        } else {
            event.setSortBeanList(mSelectGoodsAdapter.getSelectedList());
        }
        EventBus.getDefault().postSticky(event);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSelectedGoodsBinding != null) {
            mSelectedGoodsBinding.unbind();
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        Map<String, SortBean> selectedMap = getAdapter().getSelectedMap();
        int totalCount = selectedMap.size();
        for (int i = 0; i < getAdapter().getData().size(); i++) {
            SortBean sortBean = getAdapter().getData().get(i);
            SortBean bean = selectedMap.get(String.valueOf(sortBean.getSortId()));
            if (bean != null) {
                getAdapter().setData(i, bean);
            }
        }
        setTvCount(totalCount);
    }
}
